package com.keller.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keller.guli.service.edu.entity.Video;
import com.keller.guli.service.edu.feign.VodMediaService;
import com.keller.guli.service.edu.mapper.VideoMapper;
import com.keller.guli.service.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类 去调用feign中的service
 * </p>
 *
 * @author keller
 * @since 2020-07-21
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodMediaService vodMediaService;

    @Override
    public void removeMediaVideoById(String id) {
        //根据课时的id找到video
        Video video = baseMapper.selectById(id);
        String videoSourceId = video.getVideoSourceId();
        //调用feign接口删除
        vodMediaService.removeVideo(videoSourceId);
    }


    //根据章节di批量删除视频,删除视频数据库记录
    @Override
    public void removeMediaVideoByChapterId(String chapterId) throws Exception {
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id", chapterId);
        List<Video> videoList = baseMapper.selectList(videoQueryWrapper);
        List<String> videoIdList = new ArrayList<>();
        // 获取videoSourceId列表
        videoList.forEach(video -> {
            videoIdList.add(video.getVideoSourceId());
            //删除视频数据库数据记录
            baseMapper.deleteById(video.getId());
        });
        vodMediaService.removeVideoByIdList(videoIdList);

    }
}
